/*
 * 建立索引
 * 1.正排索引
 * 2.倒排索引
 * */
#pragma once
#include<iostream>
#include"util.hpp"
#include<string>
#include<vector>
#include<unordered_map>
namespace ns_index{
    //正排索引数据结构
    struct DocInfo{
        std::string title;
        std::string content;
        std::string url;
        uint16_t doc_id;
    };
    //倒排索引数据结构
    struct InvertedElem{
        std::string word;
        uint16_t doc_id;
        int weight;
        InvertedElem():weight(0){}
    };
    typedef std::vector<InvertedElem> InvertedList;
    class Index{
        private:
        std::vector<DocInfo> forward_index;
        std::unordered_map<std::string,InvertedList> inverted_index;
        private:
        //全局只有一个索引
        Index(){}
        Index(const Index& index) = delete;
        Index& operator=(const Index& index) = delete;
        public:
        static Index& GetInstance()
        {
            //静态局部变量只会被初始化一次，以后每次调用这个函数，都会是同一个index对象
            static Index index;
            return index;
        }
        public:
        DocInfo* GetForwardIndex(uint16_t doc_id) 
        {
            if(doc_id >= forward_index.size())
            {
                std::cerr<<doc_id<<" doc_id  error!"<<std::endl;
                return nullptr;
            }
            return &forward_index[doc_id];
        }
        InvertedList* GetInvertedIndex(const std::string& word)
        {
            auto pos = inverted_index.find(word);
            if(pos == inverted_index.end())
            {
                std::cerr<<word<<":not exists inverted_index"<<std::endl;
                return nullptr;
            }
            //return &inverted_index[word];
            return &(pos->second);
        }
        //建立正排和倒排索引
        bool BuildIndex(const std::string& input)
        {
            //读取文件内容
            std::ifstream ifs(input,std::ios::in|std::ios::binary);
            if(!ifs.is_open())
            {
                std::cerr<<"BuildIndex:"<<input<<"file open error"<<std::endl;
                return false;
            }
            std::string line;
            std::cout<<"read file success!"<<std::endl;
            while(std::getline(ifs,line))
            {
                //建立正排索引
                DocInfo* doc = BuildForwardIndex(line);
                if(doc == nullptr)
                {
                    std::cerr<<line<<":build forward index fail!"<<std::endl;
                    continue;
                }
                //建立倒排索引
                BuildInvertedIndex(doc);
            }
            std::cout<<"build index  success!"<<std::endl;
            return true;
        }
        void ShowDoc(DocInfo* doc)
        {
            std::cout<<"Title: "<<doc->title<<std::endl;
            std::cout<<"Content: "<<doc->content.substr(0,68)<<std::endl;
            std::cout<<"Url: "<<doc->url<<std::endl;
            std::cout<<"Id: "<<doc->doc_id<<std::endl;

        }
        DocInfo* BuildForwardIndex(const std::string& line)
        {
#define SEP "\3"
            //1.分词
            std::vector<std::string> results;
            ns_util::StringUtil::Split(line,&results,SEP);
            if(results.size() != 3)
            {
                std::cout<<line<<":split fail!"<<std::endl;
                return nullptr;
            }
            //2.构建DocInfo
            DocInfo doc;
            doc.title = results[0];
            doc.content = results[1];
            doc.url = results[2];
            doc.doc_id = forward_index.size();
            //for debug
            //ShowDoc(&doc);
            //3.返回对象
            forward_index.push_back(std::move(doc));
            //return &forward_index[forward_index.size()-1];
            return &forward_index.back();
        }
        bool BuildInvertedIndex(DocInfo* doc)
        {
            //该结构统计标题和内容中的关键词数量
            struct word_cnt{
                int title_cnt;
                int content_cnt;
                word_cnt():title_cnt(0),content_cnt(0){}
            };
            //对title分词
            std::vector<std::string> title_words;
            ns_util::JiebaUtil::CutString(doc->title,&title_words);
            //对content分词
            std::vector<std::string> content_words;
            ns_util::JiebaUtil::CutString(doc->content,&content_words);
            std::unordered_map<std::string,word_cnt> words;
            //遍历title分词结果
            for(auto& word:title_words)
            {
                //需要将关键字转化为小写
                boost::to_lower(word);
                words[word].title_cnt++;
            }
            //遍历content分词结果
            for(auto& word:content_words)
            {
                boost::to_lower(word);
                words[word].content_cnt++;
            }
#define X 10
#define Y 1
            //对哈希表words中的结果进行统计，构建倒排拉链
            for(auto& item:words)
            {
                InvertedElem elem;
                elem.word = item.first;
                elem.doc_id = doc->doc_id;
                elem.weight = item.second.title_cnt*X+item.second.content_cnt*Y;
                InvertedList& invertedlist = inverted_index[item.first];
                invertedlist.push_back(std::move(elem));
            }
            return true;
        }
    };
}
